package cn.tedu.cwgl.controller;

import cn.tedu.cwgl.entity.Bill;
import cn.tedu.cwgl.entity.Debt;
import cn.tedu.cwgl.entity.Payway;
import cn.tedu.cwgl.entity.UserInfo;
import cn.tedu.cwgl.service.BillService;
import cn.tedu.cwgl.util.Config;
import cn.tedu.cwgl.util.PageModel;
import cn.tedu.cwgl.util.Result;
import cn.tedu.cwgl.util.ResultUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bills")
public class BillController {

    @Resource
    private BillService billService;

    @RequestMapping("/getBillsToChart")
    // 作用于统计图
    public Result<Bill> findByWhereNoPage(Bill bill, HttpSession session) {
        bill = getHouseBill(bill, session);
        return billService.findByWhereNoPage(bill);
    }


    private Bill getHouseBill(Bill bill, HttpSession session) {
        UserInfo user = Config.getSessionUser(session);
        // 当登录用户为家主时，查询默认的是全家账单信息
        // 当登录用户为普通用户时，仅仅查询当前用户的账单
        if (user.getRoleid() == 2) {
            bill.setHouseid(user.getHouseid());
        } else if (user.getRoleid() == 3) {
            bill.setUserid(user.getId());
        }
        return bill;
    }

    @RequestMapping("/getBillsByWhere/{type}/{pageNo}/{pageSize}")
    public Result<Bill> getBillsByWhere(Bill bill, @PathVariable String type,
                                        @PathVariable int pageNo, @PathVariable int pageSize, HttpSession session) {


        if (!StringUtils.isEmpty(bill.getStartTime())) {
            bill.setStartTime(bill.getStartTime() + " 00:00:00");
        }
        if (!StringUtils.isEmpty(bill.getEndTime())) {
            bill.setEndTime(bill.getEndTime() + " 23:59:59");
        }
        if ("-1".equals(bill.getPayway())) {
            bill.setPayway(null);
        }
        if ("-1".equals(bill.getTitle())) {
            bill.setTitle(null);
        }
        bill.setType(type);
        bill = getHouseBill(bill, session);
        PageModel model = new PageModel<>(pageNo, bill);
        model.setPageSize(pageSize);
        return billService.findByWhere(model);
    }

    @RequestMapping("/getPayways")
    public Result<Payway> getAllPayways() {
        List<Payway> list = billService.getAllPayways();
        if (list != null && list.size() > 0) {
            return ResultUtil.success(list);
        } else {
            return ResultUtil.unSuccess();
        }
    }

    @RequestMapping("/addBill")
    @ResponseBody
    public Result addBill(Bill bill, HttpSession session) {
        int result = billService.addBill(bill, session);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }

    }

    @RequestMapping("/delBill")
    @ResponseBody
    public Result deleteBill(Bill bill) {
        int result = billService.deleteBill(bill);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }

    @RequestMapping("/updateBill")
    @ResponseBody
    public Result updateBill(Bill bill) {
        int result = billService.updateBill(bill);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }


}
